<?php
	include("gen.php");
	$cmd = get_datan("cmd");

	switch($cmd){
		case 1:
			get_cho_id();
		break;
		case 2:
			delete_cho();
		break;
		case 3:
			all_cho();
		break;
		case 4:
			update();
		break;
		case 5:
			add();
		break;
		default:
			echo "{";
			echo jsonn("result",0). ",";
			echo jsons("message","unknown command");
			echo "}";
		}


	function get_cho_id(){
			include_once("cho.php");
			$id = get_datan("idcho");
			$c = new cho();
			$row = $c -> get_cho_id($id);
			//$row = $c -> fetch();

				
			if(!$row){
				echo "not found";
				return;
			}
		
			echo "{";

				echo jsonn("result",1).",";
				echo '"cho":{';
				echo jsonn("idcho", $id).",";
				echo jsons("cho_name", $row['cho_name']).",";
				echo jsonn("subdistrict_id", $row['subdistrict_id']).",";
				echo jsonn("pin_number", $row['pin_number']);
			echo "}";
			echo "}";
		}

	function delete_cho(){
		include_once("cho.php");
		$id = get_datan("id");
		$c = new cho();
			$row = $c -> delete_cho($id);
			//$row = $c -> fetch();

			if(!$row){
				echo "not found";
				exit();
			}
			echo "{";
				echo jsonn("result",1) .",";
				echo '"cho":{';	
				echo jsons("idcho", $row['idcho']);
				echo "}";
			echo "}";
		}



	function all_cho(){
		include_once("cho.php");
			$id = get_datan("id");
			$c = new cho();
			$row = $c -> get_all_cho();
			
			if(!$row){
				echo "not found";
				exit();
			}
			echo "{";
			echo jsonn("result",1) .",";
			echo '"cho":[';
			$row = $c -> fetch();
			while($row){
			echo "{";
				echo jsonn("idcho", $row['idcho']) . ",";
				echo jsons("cho_name", $row['cho_name']) . ",";
				echo jsonn("subdistrict_id", $row['subdistrict_id']) . ",";
				echo jsonn("pin_number", $row['pin_number']);
			if($row = $c -> fetch()){
				echo "},";
			}else{
				echo "}";
			}
			}
	
		echo "]";
		echo "}";
	}
	
	function  update(){
	include_once("cho.php");
		//	$id = get_datan("idcho");
			$c = new cho();
			
			$id = get_datan ("id");
			$choName = get_data("choName");
			$subD = get_datan ("subD");
			$pinN = get_datan ("pinN");
			//$c -> update_cho($id,$choName,$subD,$pinN);
			
			if(!$c->update_cho($id,$choName,$subD,$pinN)){
			echo "{";
				echo jsonn ("result",0).",";
				echo jsons ("message","update error");
			echo "}";
			return;
			}
			echo "{";
				echo jsonn ("result",1).",";
				echo jsons ("message","update successful");
			echo "}";
			}
			
	function add(){
	include_once("cho.php");
			$c = new cho();
			$choName = get_data("choName");
			$subD = get_datan ("subD");
			$pinN = get_datan ("pinN");
				
			
			if(!$c->add_cho($choName,$subD,$pinN)){
			echo "{";
				echo jsonn ("result",0).",";
				echo jsons ("message","update error");
			echo "}";
			return;
			}
			echo "{";
			echo jsonn ("result",1).",";
				echo jsons ("message","update successful");
			echo "}";
			
			
	}


